namespace EyeSoft.Wpf.Facilities.Conventions
{
	using System;
	using System.Reflection;
	using System.Windows;

	using EyeSoft.Conventions;

	public class DefaultViewToViewModelConvention : TypeConvention<Window, ViewModel>
	{
		protected override Type TryMapTo(Type viewType)
		{
			var assemblyName =
				new AssemblyName(viewType.Assembly.FullName)
					.Name.TrimEnd(".Wpf".ToCharArray());

			assemblyName = string.Concat(assemblyName, ".ViewModels");

			var viewModelName = viewType.Name.Replace("Window", "ViewModel");

			var vievModelTypeName =
				"{ViewModelsAssemblyName}.{ViewModelName}, {ViewModelsAssemblyName}"
					.NamedFormat(assemblyName, viewModelName);

			var vmType = Type.GetType(vievModelTypeName, true);

			return vmType;
		}
	}
}